from django.shortcuts import render

# Create your views here.
from rest_framework import viewsets, mixins, permissions
from rest_framework_jwt.authentication import JSONWebTokenAuthentication
from main.models import Forks, Articels, ContentImages, Remarks
from main.serializers import (
    ForksSerializer,
    ArticelsSerializer_Get,
    ArticelsSerializer_Post,
    ContentImagesSerializer,
    RemarksSerializer,
)
from rest_framework.pagination import LimitOffsetPagination


class ForksViewsets(viewsets.ModelViewSet):

    queryset = Forks.objects.all()
    serializer_class = ForksSerializer
    pagination_class = LimitOffsetPagination
    # authentication_classes = [JSONWebTokenAuthentication]


class ArticelsViewsets(viewsets.ModelViewSet):
    queryset = Articels.objects.all()
    pagination_class = LimitOffsetPagination

    def get_queryset(self):
        fork = self.request.query_params.get("fork", None)
        if fork is not None:
            queryset = Articels.objects.filter(forkby=fork)
        else:
            queryset = Articels.objects.all().order_by("pk")
        return queryset

    def get_serializer_class(self):
        if self.action == "create" or self.action == "update":
            serializer_class = ArticelsSerializer_Post
        else:
            serializer_class = ArticelsSerializer_Get
        return serializer_class

    # authentication_classes = [JSONWebTokenAuthentication]


class RemarksViewsets(viewsets.ModelViewSet):
    queryset = Remarks.objects.all()
    serializer_class = RemarksSerializer
    permission_classes = [permissions.IsAuthenticated]
    authentication_classes = [JSONWebTokenAuthentication]


class ContentImagesViewsets(viewsets.ModelViewSet):
    queryset = ContentImages.objects.all()
    serializer_class = ContentImagesSerializer
    pagination_class = LimitOffsetPagination
    # authentication_classes = [JSONWebTokenAuthentication]
    # def perform_create(self, serializer):
    #     serializer.save(owner=self.request.user)

    def get_queryset(self):
        pok = self.request.query_params.get("ownerpk", None)
        if pok is not None:
            queryset = ContentImages.objects.filter(owner=pok)
        else:
            queryset = ContentImages.objects.all().order_by("pk")
        return queryset

    def post(self, request):
        serializer = ContentImagesSerializer(data=request.data)
        if serializer.is_valid():
            name = str(time.time()).split(".")[0].strip()
            # suffix = request.data["suffix"].split(r'"')[1].strip()
            avatar_name = "avatar/{}/{}".format(request.user.username, name)
            avatar = Image.open(request.data["contentimages"])  # 获得图片
            if not os.path.exists("media/contentimages/" + request.user.username):
                os.makedirs("media/contentimages/" + request.user.username)
            avatar_file_path = os.path.join(settings.MEDIA_ROOT, avatar_name).replace(
                "\\", "/"
            )
            avatar.save(avatar_file_path)
